<template>
    <section>
        <div style="margin-bottom:5px;">
            <el-input placeholder="请输入内容" size="small" v-model="searchKey" style="width: 250px;">
                <el-button slot="append" size="small" icon="el-icon-search" @click="getDataList"></el-button>
            </el-input>

        </div>
        <el-table ref="multipleTable" :data="dataList" border tooltip-effect="dark" style="width: 100%"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" label="序号">
            </el-table-column>

            <el-table-column prop="url" label="URL地址">
            </el-table-column>
            <el-table-column prop="clientIp" label="请求IP">
            </el-table-column>
            <el-table-column prop="requestMethod" label="请求方法">
            </el-table-column>
            <el-table-column prop="redirectUrl" label="转发后路径">
            </el-table-column>

            <el-table-column label="访问人">
                <template slot-scope="scope">
                    <id-to-name :id="scope.row.createBy"
                                url="sys/org/common/getNameById/orgUserCommonService"></id-to-name>
                </template>
            </el-table-column>
            <el-table-column label="开始时间">
                <template slot-scope="scope">{{ scope.row.startTime }}</template>
            </el-table-column>
            <el-table-column label="结束时间">
                <template slot-scope="scope">{{ scope.row.endTime }}</template>
            </el-table-column>
            <el-table-column label="耗时" width="100">
                <template slot-scope="scope">{{ scope.row.costTime }}</template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button size="mini" type="primary" @click="openDialog(scope.row.id)">查看</el-button>
                        <el-button size="mini" type="danger" @click="removeItem(scope.row.id)">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div style="text-align: right;background: rgb(239, 242, 247);border: 1px solid #E5E9F2;">
            <el-pagination style="line-height: 30px; height: 30px;" @size-change="handleSizeChange"
                           @current-change="handleCurrentChange" :current-page="currentPage"
                           :page-sizes="[10, 20, 30, 50]" :page-size="pageSize"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <div v-if="dialogFormVisibleAdd">
            <el-dialog :title="dialogAddOrEdit" :visible="dialogFormVisibleAdd" :modal="dialogFormVisibleAdd"
                       :before-close="openAddDialogClose">

                <el-form label-width="100px" :rules="formAddRules" :model="formAdd" ref="formAdd" disabled>

                    <el-row>
                        <el-col :span=12>
                            <el-form-item label="访问IP" prop="clientIp">
                                <el-input v-model="formAdd.clientIp" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span=12>
                            <el-form-item label="URL地址" prop="url">
                                <el-input v-model="formAdd.url" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>


                    </el-row>

                    <el-row>
                        <el-col :span=12>
                            <el-form-item label="请求方法" prop="requestMethod">
                                <el-input v-model="formAdd.requestMethod" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span=12>
                            <el-form-item label="请求参数" prop="requestParams">
                                <el-input v-model="formAdd.requestParams" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                    </el-row>

                    <el-row>

                        <el-col :span=12>
                            <el-form-item label="返回状态" prop="responseStatus">
                                <el-input v-model="formAdd.responseStatus" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span=12>
                            <el-form-item label="访问人" prop="responseStatus" v-if="formAdd.createBy!=null">
                                <id-to-name :id="formAdd.createBy"
                                            url="sys/org/common/getNameById/orgUserCommonService"></id-to-name>
                            </el-form-item>
                        </el-col>

                    </el-row>
                    <el-row>

                        <el-col :span=24>
                            <el-form-item label="返回结果" prop="responseResult">
                                <el-input type="textarea" v-model="formAdd.responseResult"
                                          style="width: 100%;height:100%"></el-input>
                            </el-form-item>
                        </el-col>


                    </el-row>
                    　
                    <el-row>


                        <el-col :span=12>
                            <el-form-item label="访问时间" prop="startTime">
                                <el-input v-model="formAdd.startTime" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span=12>
                            <el-form-item label="访问时间" prop="endTime">
                                <el-input v-model="formAdd.endTime" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>


                        <el-col :span=12>
                            <el-form-item label="耗时" prop="costTime">
                                <el-input v-model="formAdd.costTime" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span=12>

                        </el-col>
                    </el-row>

                <el-row>
                <el-col :span=24>
                    <el-form-item label="转发后路径" prop="costTime">
                        <el-input v-model="formAdd.redirectUrl" style="width: 100%"></el-input>
                    </el-form-item>
                </el-col>
                </el-row>

                </el-form>
                <el-row>


                    <el-col :span=24 align="center">
                        <el-button @click="openAddDialogClose">关闭</el-button>
                    </el-col>
                </el-row>

            </el-dialog>
        </div>
    </section>
</template>
<style>

</style>
<script>
    import {AjaxByAll} from '@/api/api';
    import idToName from '@/views/common/IdToName.vue';

    export default {
        data() {

            return {
                dataList: [],
                multipleSelection: [],
                dialogFormVisibleAdd: false,
                labelPosition: 'left',

                formAdd: {
                    id: null,
                    url: null,
                    clientIp: null,
                    requestMethod: null,
                    requestParams: null,
                    responseStatus: null,
                    endTime: null,
                    startTime: null,
                    costTime: null,
                    createBy: null,
                    redirectUrl: null
                },
                defaultData: null,
                pageSize: 10,
                currentPage: 1,
                total: 0,
                dialogAddOrEdit: '',
                searchKey: null,
                formAddRules: {}
            }
        },

        methods: {
            openDialog(id) {


                this.dialogAddOrEdit = '查看系统访问日志';
                var urls = 'gateway/log/access/' + id;
                var method = 'get';
                var form = {};
                AjaxByAll(method, urls, form).then(data => {
                    if (data.code === 200) {
                        this.formAdd = data.data;
                    } else {
                        this.$message.error(data.msg);
                    }
                });

                this.dialogFormVisibleAdd = true;
            },
            openAddDialog() {
                this.dialogAddOrEdit = '新建系统访问日志';
                // 送出後
                this.$data.formAdd = Object.assign(this.$data.formAdd, this.defaultData);
                this.dialogFormVisibleAdd = true;
            },
            openAddDialogClose() {

                this.dialogFormVisibleAdd = false;
            },
            getDataList() {
                let self = this;
                let form = {
                    currentPage: self.currentPage,
                    pageSize: self.pageSize,
                    keyword: self.searchKey,
                };
                let urls = 'gateway/log/access/query';
                AjaxByAll('post', urls, form).then(data => {
                    if (data.code === 200) {
                        self.total = data.data.total * 1;

                        self.dataList = data.data.records;
                    }
                    this.loading = false;
                });
            },


            removeItem(model) {
                this.$confirm('确认移除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    AjaxByAll('delete', 'gateway/log/access/' + model.id, null
                    ).then(data => {
                        if (data.code === 200
                        ) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            this.getDataList();
                        } else {
                            this.$message({
                                message: '操作失败：' + data.msg,
                                type: 'error'
                            });
                        }

                    })
                    ;
                }).catch(() => {
                    }
                )
                ;
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    })
                    ;
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.getDataList();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.getDataList();
            }
        },
        created() {
            this.defaultData = JSON.parse(JSON.stringify(this.formAdd));
        },
        mounted: function () {
            this.getDataList();
        }, components: {

            idToName
        }
    }
</script>
